﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;
using System.Windows.Forms;

namespace HaoPeng
{
    /// <summary>
    /// 日志辅助类
    /// </summary>
    public class LogHelper
    {
        private static object m_LockObject = new object();

        /// <summary>
        /// 播放输出日志
        /// </summary>
        /// <param name="msg"></param>
        public static void WritePlayLog(string msg)
        {
            string path = Application.StartupPath + "\\playLog\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
            msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + ":\t " + msg;
            WriteLog(msg, path, true, Encoding.Unicode);
        }

        /// <summary>
        /// 注液信息记录
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="bno"></param>
        /// <param name="testType"></param>
        public static void WriteZYLog(string msg, string bno, int testType)
        {
            string dir = Application.StartupPath + "\\ZYLog\\";
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            string logPath = dir + bno + "_" + testType + ".log";
            WriteLog(msg, logPath, true, Encoding.Unicode);
        }

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="logType"></param>
        public static void WriteLog(string msg)
        {
            string logPath = Application.StartupPath;
            string path = logPath + "\\{0}\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
            msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + ":\t " + msg;
            //输出串口通讯日志
            path = string.Format(path, "serialPort");
            WriteLog(msg, path, true, Encoding.Unicode);
        }

        /// <summary>
        /// 日志写入到批定的文件路径下
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="filePath"></param>
        public static bool WriteLog(string msg, string filePath,bool IsAppend,Encoding code)
        {
            bool isSuccess = false;
            string folder = Path.GetDirectoryName(filePath);
            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }
            lock (m_LockObject)
            {
                StreamWriter sw = null;
                try
                {
                    sw = new StreamWriter(filePath, IsAppend, code);
                    sw.WriteLine(msg);
                    isSuccess = true;
                }
                catch (Exception)
                {
                    isSuccess = false;
                }
                finally
                {
                    if (sw != null)
                    {
                        sw.Close();
                    }
                }
            }
            return isSuccess;
        }
    }
}
